﻿/*Author：Lary Pan(itolmqj123@gmail.com)
 *Date  ：2016/5/20
 *Tips  ：事件管理
 * */
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

public delegate void EventListener(params object[] args);

public class EventManager : ISingleton<EventManager>, IManager
{
	private Dictionary<CEvent, List<EventListener>> _listeners;

    public void Init()
    {
        _listeners = new Dictionary<CEvent, List<EventListener>>();
	}

	public void DoDestroy() 
    {
        _listeners.Clear();
        //_listeners = null;
    }


    public void AddListener(CEvent key, EventListener listener)
    {
		if (!_listeners.ContainsKey(key))
        {
			_listeners.Add(key, new List<EventListener>());
        }
		_listeners[key].Add(listener);
    }


    public void RemoveLisener(CEvent key, EventListener listener)
    {
		if (_listeners.ContainsKey(key))
			_listeners[key].Remove(listener);
    }

    public void RemoveLisener(CEvent key)
    {
        _listeners.Remove(key);
    }

    //派发事件
    public void DispatchMsg(CEvent key, params object[] param)
    {
		List<EventListener> list = null;
		if (_listeners.TryGetValue(key, out list))
        {
			List<EventListener> tempList = new List<EventListener>(list);
			foreach (EventListener _delegate in tempList)
            {
				_delegate(param);
            }
        }
    }

    /// <summary>
    /// 延时派发事件
    /// </summary>
    /// <param name="time"></param>
    /// <param name="key"></param>
    /// <param name="param"></param>
    public void DispatchMsgDelay(float time, CEvent key, params object[] param)
    {
        CGameApp.Instance.StartCoroutine(Delay(time, key, param));
    }

    IEnumerator Delay(float time, CEvent key, params object[] param)
    {
        yield return new WaitForSeconds(time);
        this.DispatchMsg(key, param);
    }
}



/// <summary>
/// 事件类型
/// </summary>
public enum CEvent
{
    OpenUI = 1,         //打开UI
    CloseUI,            //关闭UI
   
    LoadActorFinish,    //加载Actor完成
    NextTurn,           //战斗下一回合
    BattleMsg,          //战斗数据包
    BattleEffect,       //战斗特效
    BattleShow,         //战斗显示(冒血或技能名称)
    BattleOver,         //战斗结束
   
}
